/**
 * Sample ContentProvider
 * @author YUN
 */
package a.db;

import a.db.MyDatabase.SectionTest;
import a.db.MyDatabase.Test;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

public class MyContentProvider extends ContentProvider {

	private static final int TEST = 1;
	private static final int SECTION_TEST = 2;

	private static final UriMatcher sUriMatcher;
	static {
		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		sUriMatcher.addURI(MyDatabase.AUTHORITY, Test.TABLE_NAME, TEST);
		sUriMatcher.addURI(MyDatabase.AUTHORITY, SectionTest.TABLE_NAME, SECTION_TEST);
	}

	private MySQLiteOpenHelper mSqlite;

	//
	@Override
	public boolean onCreate() {
		mSqlite = new MySQLiteOpenHelper(getContext());
		return true;
	}

	@Override
	public String getType(Uri uri) {
		switch (sUriMatcher.match(uri)) {
		case TEST:
			return Test.TEST_TYPE;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues initialValues) {
		//
		if (initialValues == null) {
			throw new IllegalArgumentException("initialValues is null.");
		}

		long id;
		SQLiteDatabase db = mSqlite.getWritableDatabase();

		switch (sUriMatcher.match(uri)) {
		case TEST:
			id = db.insert(Test.TABLE_NAME, null, initialValues);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		Uri insertedUri = null;
		if (0 < id) {
			insertedUri = ContentUris.withAppendedId(uri, id);
			getContext().getContentResolver().notifyChange(insertedUri, null);
		}

		return insertedUri;
	}

	@Override
	public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
		int count = 0;
		SQLiteDatabase db = mSqlite.getWritableDatabase();

		switch (sUriMatcher.match(uri)) {
		case TEST:
			count = db.update(Test.TABLE_NAME, values, where, whereArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		if (0 < count) {
			getContext().getContentResolver().notifyChange(uri, null);
		}

		return count;
	}

	@Override
	public int delete(Uri uri, String where, String[] whereArgs) {
		int count = 0;
		SQLiteDatabase db = mSqlite.getWritableDatabase();

		switch (sUriMatcher.match(uri)) {
		case TEST:
			count = db.delete(Test.TABLE_NAME, where, whereArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		if (0 < count) {
			getContext().getContentResolver().notifyChange(uri, null);
		}

		return count;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String where, String[] whereArgs, String sortOrder) {
		String sql = null;

		switch (sUriMatcher.match(uri)) {
		case TEST:
			sql = SQLiteQueryBuilder.buildQueryString(false, Test.TABLE_NAME, projection, where, null, null, sortOrder, null);
			break;
		case SECTION_TEST:
			sql = SQLiteQueryBuilder.buildQueryString(false, SectionTest.TABLE_NAME, projection, where, null, null, sortOrder, null);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		Cursor c = mSqlite.getReadableDatabase().rawQuery(sql, whereArgs);
		if (c != null) {
			c.setNotificationUri(getContext().getContentResolver(), uri);
		}
		return c;
	}
}
